package cn.ecut.controller;


import cn.ecut.model.Message;
import cn.ecut.model.Xiangmu;
import cn.ecut.model.Yuangong;
import cn.ecut.service.XiangmuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.*;
import java.util.List;

@Controller
@RequestMapping("/xiangmu")
public class XiangmuController {

    @Autowired
    private XiangmuService xiangmuService;

    @ResponseBody
    @RequestMapping("/add")
    public Message add(Xiangmu xiangmu){
        Message message = new Message();
        xiangmu.setXiangmubianma(xiangmuService.getxiangmuBianma());
        System.out.println(xiangmu);
        int n = xiangmuService.insert(xiangmu);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/show")
    public HashMap<String, Object> show(){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map = (HashMap)xiangmuService.getXiangmuMessage();
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/qiyong")
    public Message qiyong(Xiangmu xiangmu){
        Message message = new Message();
        if(xiangmu.getQiyongzhuangtai()==0){
            xiangmu.setQiyongzhuangtai(1);
        }else {
            xiangmu.setQiyongzhuangtai(0);
        }
        int n = xiangmuService.updateQiyong(xiangmu);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }

    @ResponseBody
    @RequestMapping("/select")
    public HashMap<String, Object> select(String searchResult){
        System.out.println("进入到了 /select");
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Xiangmu> list = new ArrayList<>();
        list = xiangmuService.advanceSelect(searchResult);
        Integer count = list.size();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Message delete(String xiangmubianma){
        Message message = new Message();
        int n = xiangmuService.deletexiangmu(xiangmubianma);
        if(n == 1){
            message.setFlag(true);
            return message;
        }else {
            message.setFlag(false);
            return message;
        }
    }
    @ResponseBody
    @RequestMapping("/update")
    public Message update(Xiangmu xiangmu){
        int n = xiangmuService.updateByPrimaryKey(xiangmu);
        Message message = new Message();
        if (n == 1) {
            message.setFlag(true);
            return message;
        } else {
            message.setFlag(false);
            return message;
        }
    }
}
